import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { fileURLToPath, URL } from 'node:url'

import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { loadEnv } from 'vite'
import topLevelAwait from 'vite-plugin-top-level-await'

// @ts-ignore
import { viteProxy } from './scripts/temp/b.mjs'

// https://vitejs.dev/config/
export default function ({ mode }: any) {
	const root = process.cwd()
	const env = loadEnv(mode, root)
	return {
		plugins: [
			vue(),
			vueJsx(),
			AutoImport({
				imports: [
					'vue',
					{
						'naive-ui': [
							'useDialog',
							'useMessage',
							'useNotification',
							'useLoadingBar'
						]
					}
				]
			}),
			Components({
				extensions: ['vue'], // , 'tsx', 'jsx'
				resolvers: [NaiveUiResolver()],
				deep: true
			}),
			topLevelAwait({
				promiseExportName: '__tla',
				promiseImportName: (i: any) => `__tla_${i}`
			})
		],
		css: {
			// css预处理器
			preprocessorOptions: {
				less: {
					charset: false
					// additionalData: '@import "./src/assets/style/global.less";',
				}
			}
		},
		resolve: {
			alias: {
				'@': fileURLToPath(new URL('./src', import.meta.url))
			}
		},
		server: {
			port: 9080,
			host: '0.0.0.0',
			// open: true,
			proxy: {
				[env.VITE_APP_API_PREFIX]: {
					target: env.VITE_APP_API_BASE_URL,
					changeOrigin: true,
					secure: false,
					configure: viteProxy,
					selfHandleResponse: true,
				}
			}
		}
	}
}
